class Game {
  constructor(el) {
    /*  this.snakeStart = document.querySelector('button.start')
     console.log(this.snakeStart);
     this.snakeStop = document.querySelector('button.stop') */
    this.el = document.querySelector(el)
    //地图
    this.map = new Map(this.el)
    this.map.create()
    //食物
    this.food = new Food(this.el)
    this.food.create()
    //玩蛇
    this.snake = new Snake(this.el)
    this.snake.create()
    this.t
  }
  start() {
    this.t = setInterval(() => {
      this.snake.move()
      let snakeX = this.snake.snakeObj[0].x * 20
      let snakeY = this.snake.snakeObj[0].y * 20
      let foodX = this.food.foodX * 20
      let foodY = this.food.foodY * 20
      console.log(snakeX);
      console.log(this.t);
      if (snakeX < 0 || snakeX > 780 || snakeY < 0 || snakeY > 380) {
        clearInterval(this.t)
        alert('GG')
      }
      if (snakeX == foodX && snakeY == foodY) {
        console.log(11);
        let last = this.snake.snakeObj[this.snake.snakeObj.length - 1]
        this.snake.snakeObj.push({
          x: last.x,
          y: last.y,
          color: last.color,
          el: null
        })
        this.snake.create()
        this.food.move()
      }
    }, 80)
    document.addEventListener('keydown', (evt) => {
      let e = evt || window.event
      //console.log(e.keyCode);
      switch (e.keyCode) {
        case 38:
          this.snake.direction = 'top'
          break;
        case 40:
          this.snake.direction = 'bottom'
          break;
        case 37:
          this.snake.direction = 'left'
          break;
        case 39:
          this.snake.direction = 'right'
          break;
        default:
          break;
      }
    })
  }
  stop() {
    clearInterval(this.t)
  }
}